package com.tj.bo.user.server;

import com.fqgj.common.utils.StringUtils;
import com.tj.bo.user.client.UserWanningListService;
import com.tj.bo.user.server.dao.UserAccessDao;
import com.tj.bo.user.server.dao.UserWanningListDao;
import com.tj.bo.user.server.entity.UserAccessEntity;
import com.tj.bo.user.server.entity.UserWanningListEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2020-08-21
 * Time: 15:47
 */
@Service
public class UserWanningListServiceImpl implements UserWanningListService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserWanningListServiceImpl.class);

    @Autowired
    private UserWanningListDao wanningListDao;

    @Autowired
    private UserAccessDao userAccessDao;

    @Override
    public void addUser(String userCode, Integer type) {


        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userCode);
        if (userAccessEntity == null) {
            LOGGER.error("== 用户不存在 ==,userCode:{}", userCode);
            return;
        }

        if (StringUtils.isEmpty(userAccessEntity.getDeviceId())) {
            LOGGER.error("== device id not exist ==,userCode:{}", userCode);
            return;
        }

        UserWanningListEntity userWarningByCondition = wanningListDao.getUserWarningByCondition(userAccessEntity.getDeviceId(), userAccessEntity.getUserCode(), type == null ? 0 : type);
        if(userWarningByCondition != null){
            return;
        }
        UserWanningListEntity userWanningListEntity = new UserWanningListEntity()
                .setUserCode(userCode)
                .setType(type == null ? 0 : type)
                .setDeviceId(userAccessEntity.getDeviceId());
        wanningListDao.insert(userWanningListEntity);
    }

    @Override
    public boolean isGrey(String userCode) {
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userCode);
        UserWanningListEntity latestOneByDeviceId = getLatestOneByDeviceId(userAccessEntity.getDeviceId());
        if (latestOneByDeviceId == null) {
            return false;
        }
        return 0 == latestOneByDeviceId.getType();
    }

    @Override
    public boolean isBlack(String userCode) {
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userCode);
        UserWanningListEntity latestOneByDeviceId = getLatestOneByDeviceId(userAccessEntity.getDeviceId());
        if (latestOneByDeviceId == null) {
            return false;
        }
        return 1 == latestOneByDeviceId.getType();
    }

    @Override
    public boolean isWhite(String userCode) {
        UserAccessEntity userAccessEntity = userAccessDao.selectUserAccessByUserCode(userCode);
        UserWanningListEntity latestOneByDeviceId = getLatestOneByDeviceId(userAccessEntity.getDeviceId());
        if (latestOneByDeviceId == null) {
            return false;
        }
        return 2 == latestOneByDeviceId.getType();
    }


    private UserWanningListEntity getLatestOneByDeviceId(String deviceId) {
        if (StringUtils.isEmpty(deviceId)) {
            return null;
        }

        UserWanningListEntity latestOneByDeviceId = wanningListDao.getLatestOneByDeviceId(deviceId);
        if (latestOneByDeviceId == null) {
            return null;
        }

        return latestOneByDeviceId;
    }
}
